/**
 * Author: w景洋.
 * Time: 2016/4/19 0019
 */
angular.module('service.shopmenu', [
        "ngStorage"
    ])
    //获取店铺菜单
    .service('ShopMenuService', ['$localStorage', 'MenuGoodsService', '$resource', 'ShopService','MallAuthService', function ($localStorage, MenuGoodsService, $resource, ShopService,MallAuthService) {
        var $storage = $localStorage.$default({
            shopcart: {},
        });
        var shop_goods_list = {};
        var menuList = {};
        //根据店铺id获取店铺菜单
        this.getMenuByShopid = function (shopid) {
            if (menuList[shopid]) {
                return menuList[shopid];
            }
            var menu = MenuGoodsService.getMenuByShopid(shopid);
            menu.$promise.then(function (goodsList) {
                for (var i = 0; i < goodsList.length; i++) {
                    var goodsitem = goodsList[i];
                    for (var j = 0; j < goodsitem.goods.length; j++) {
                        var good = goodsitem.goods[j];
                        good.num = 0;
                    }
                }
            });
            menuList[shopid] = menu;
            return menu;
        }

        //新增购买
        this.addGood = function (shopid, cid, goodsid) {
            //shop_goods_list = $storage.shopmenu;
            var goodsList = shop_goods_list[shopid];
            var num = 0;
            if (!shop_goods_list[shopid]) {
                shop_goods_list[shopid] = [];
            }
            if (goodsList == undefined) {
                goodsList = menuList[shopid];
            }
            var goodsitem = null;
            for (var i = 0; i < goodsList.length; i++) {
                if (goodsList[i].cid == cid && goodsList[i].goodsid == goodsid) {
                    goodsitem = goodsList[i];
                    break;
                }
            }
            if (goodsitem == null) {
                goodsList = menuList[shopid];
                for (var k = 0; k < goodsList.length; k++) {
                    if (goodsList[k].cid == cid && goodsList[k].goodsid == goodsid) {
                        if (goodsList[k].num < 999) {
                            goodsList[k].num++;
                            num = goodsList[k].num;
                        } else {
                            num = goodsList[k].num;
                            input_alert("超出购买范围");
                        }
                        break;
                    }
                    if (goodsList[k].cid == cid && goodsList[k].goods != undefined) {
                        for (var m = 0; m < goodsList[k].goods.length; m++) {
                            var good = goodsList[k].goods[m];
                            if (good.goodsid == goodsid) {
                                if (good.num < 999) {
                                    good.num++;
                                    num = good.num;
                                } else {
                                    num = goodsList[k].num;
                                    input_alert("超出购买范围");
                                }
                                break;
                            }
                        }
                    }
                }
                detail = this.getGoodsDetail(shopid, cid, goodsid, num);
                shop_goods_list[shopid].push(detail.goods);
            } else {
                for (var k = 0; k < goodsList.length; k++) {
                    if (goodsList[k].cid == cid && goodsList[k].goodsid == goodsid) {
                        if (goodsList[k].num < 999) {
                            goodsList[k].num++;
                            num = goodsList[k].num;
                        } else {
                            num = goodsList[k].num;
                            input_alert("超出购买范围");
                        }
                        break;
                    }
                }
                var detail = this.getGoodsDetail(shopid, cid, goodsid, num);
            }
        }

        //减少购买
        this.removeGood = function (shopid, cid, goodsid) {
            var goodsList = shop_goods_list[shopid];
            var goods_list = menuList[shopid];
            if (!shop_goods_list[shopid]) {
                shop_goods_list[shopid] = [];
            }
            if (goodsList == undefined) {
                goodsList = menuList[shopid];
            }
            var goodsitem = null;
            for (var i = 0; i < goodsList.length; i++) {
                goodsitem = goodsList[i];
                if (goodsitem.goods == undefined && goodsitem.goodsid == goodsid) {
                    goodsitem.num--;
                    if (goodsitem.num < 1) {
                        goodsList.splice(i, 1)
                    }
                    for (var k = 0; k < goods_list.length; k++) {
                        if (goods_list[k].cid == cid) {
                            for (var m = 0; m < goods_list[k].goods.length; m++) {
                                var good = goods_list[k].goods[m];
                                if (good.goodsid == goodsid) {
                                    good.num = goodsitem.num;
                                }
                            }
                        }
                    }
                    break;
                }
            }
            if (goodsList.length == 0) {
                delete shop_goods_list[shopid];
            } else {
                shop_goods_list[shopid] = goodsList;
            }
        }

        //输入框失去焦点
        this.inputBlur = function (shopid, cid, goodsid, num) {
            var goodsList = shop_goods_list[shopid];
            if (!shop_goods_list[shopid]) {
                shop_goods_list[shopid] = [];
            }
            if (goodsList == undefined) {
                goodsList = menuList[shopid];
            }
            var goodsitem = null;
            for (var i = 0; i < goodsList.length; i++) {
                if (goodsList[i].cid == cid && goodsList[i].goodsid == goodsid) {
                    goodsitem = goodsList[i];
                    break;
                }
            }
            if (goodsitem == null) {
                if (shop_goods_list[shopid].length == 0) {
                    delete shop_goods_list[shopid];
                }
            } else {
                for (var k = 0; k < goodsList.length; k++) {
                    if (goodsList[k].cid == cid && goodsList[k].goodsid == goodsid) {
                        if (num == null || num == undefined) {
                            num = 0;
                            input_alert("输入有误");
                        }
                        goodsList[k].num = num;
                        if (num == 0) {
                            goodsList.splice(k, 1);
                        }
                        break;
                    }
                }
                this.getGoodsDetail(shopid, cid, goodsid, num);
                if (goodsList.length == 0) {
                    delete shop_goods_list[shopid];
                } else {
                    shop_goods_list[shopid] = goodsList;
                }
            }
        }

        //监听输入框不规范输入
        this.inputChange = function (shopid, cid, goodsid, num) {
            var goodsList = shop_goods_list[shopid];
            if (!shop_goods_list[shopid]) {
                shop_goods_list[shopid] = [];
            }
            if (goodsList == undefined) {
                goodsList = menuList[shopid];
            }
            var goodsitem = null;
            for (var i = 0; i < goodsList.length; i++) {
                if (goodsList[i].cid == cid && goodsList[i].goodsid == goodsid) {
                    goodsitem = goodsList[i];
                    break;
                }
            }
            if (goodsitem == null) {
                goodsList = menuList[shopid];
                for (var k = 0; k < goodsList.length; k++) {
                    if (goodsList[k].cid == cid) {
                        for (var m = 0; m < goodsList[k].goods.length; m++) {
                            var good = goodsList[k].goods[m];
                            if (good.goodsid == goodsid) {
                                if (num > 999) {
                                    num = 999;
                                    input_alert("超出购买范围");
                                    good.num = num;
                                } else if (num < 0) {
                                    num = 0;
                                    good.num = num;
                                    input_alert("超出购买范围");
                                }
                                detail = this.getGoodsDetail(shopid, cid, goodsid, num);
                                shop_goods_list[shopid].push(detail.goods);
                                if (shop_goods_list[shopid].length == 0) {
                                    delete shop_goods_list[shopid];
                                }
                                break;
                            }
                        }
                    }
                }
            } else {
                for (var k = 0; k < goodsList.length; k++) {
                    if (goodsList[k].cid == cid && goodsList[k].goodsid == goodsid) {
                        if (num > 999) {
                            num = 999;
                            input_alert("超出购买范围");
                        } else if (num < 0) {
                            num = 0;
                            input_alert("超出购买范围");
                        }
                        goodsList[k].num = num;
                        if (num == 0) {
                            goodsList.splice(k, 1);
                        }
                        break;
                    }
                }
                var detail = this.getGoodsDetail(shopid, cid, goodsid, num);
                if (goodsList.length == 0) {
                    delete shop_goods_list[shopid];
                } else {
                    shop_goods_list[shopid] = goodsList;
                }
            }
        }
        //获取总价和产品总数
        this.gettotalpriceandcount = function (shopid) {
            var count = 0;
            var integration = 0;
            var totalprice = 0;
            var goods_list = [];
            if (shop_goods_list[shopid]) {
                goods_list = shop_goods_list[shopid];
            }
            for (var i = 0; i < goods_list.length; i++) {
                var goodsitem = goods_list[i];
                if (goodsitem.num > 0) {
                    totalprice += goodsitem.price * goodsitem.num;
                    count += 1 * goodsitem.num;
                    integration += (goodsitem.price * goodsitem.num) / 100;
                }
            }
            return {totalprice: totalprice, count: count, integration: integration};
        };

        //获取店铺购物列表
        this.getShopCarByShopid = function (shopid) {
            var goods_list = [];
            if (shop_goods_list[shopid]) {
                goods_list = shop_goods_list[shopid];
            }
            return goods_list;
        }
        //移除某个id的商品
        this.cleanGoodById = function (shopid, cid, goodsid) {
            var goodsList = menuList[shopid];
            if (goodsList == undefined) {
                goodsList = shop_goods_list[shopid];
                if(goodsList == undefined){
                    goodsList = [];
                }
            }
            var goodsitem = null;
            for (var i = 0; i < goodsList.length; i++) {
                goodsitem = goodsList[i];
                if (goodsitem.goods == undefined && goodsitem.goodsid == goodsid) {
                    goodsitem.num = 0;
                    break;
                }
                if (goodsitem.goods != undefined && goodsitem.cid == cid) {
                    for (var j = 0; j < goodsitem.goods.length; j++) {
                        var good = goodsitem.goods[j];
                        if (good.goodsid == goodsid) {
                            good.num = 0;
                            break;
                        }
                    }
                    break;
                }
            }
        }

        //移除某个商店所有的商品信息
        this.removeShopById = function (shopid) {
            var goodsList = shop_goods_list[shopid];
            var goods_list = menuList[shopid];
            var goodsitem = null;
            for (var i = goodsList.length - 1; i >= 0; i--) {
                goodsList.splice(i, 1);
                break;
            }
            for (var i = 0; i < goods_list.length; i++) {
                goodsitem = goods_list[i];
                if (goodsitem.goods == undefined) {
                    goodsitem.num = 0;
                    break;
                }
                for (var j = 0; j < goodsitem.goods.length; j++) {
                    var good = goodsitem.goods[j];
                    good.num = 0;
                }
            }
            delete shop_goods_list[shopid];
        }
        //店铺所有商品立即购买操作
        this.buyNowShopById = function (shopid) {
            var goodscontent = this.gettotalpriceandcount(shopid);
            var shopinfo = ShopService.queryShopByShopid(shopid);
            var shopList = [];
            var list = shop_goods_list[shopid];
            if(list!=undefined){
                var shop_goods = {
                    shopid: shopid,
                    shop: shopinfo,
                    list: list,
                    goodscontent: goodscontent,
                    buyWay: "立即购买",
                };
                shopList.push(shop_goods);
            }
            return shopList;
        }
        //将店铺所有商品加入购物车操作
        this.addToCartShopById = function (shopid) {
            var userId = MallAuthService.getUserid();
            var hasProp = false;
            var goodsList = shop_goods_list[shopid];
            var goods_list = [];
            if ($storage.shopcart[shopid] != undefined) {
                var shopCartList = $storage.shopcart[shopid];
                hasProp = true;
            }
            for (var m = 0; m < goodsList.length; m++) {
                goods_list.push(goodsList[m]);
            }
            if (hasProp == true) {
                for (var i = 0; i < shopCartList.length; i++) {
                    for (var j = 0; j < goods_list.length; j++) {
                        if (shopCartList[i].goodsid == goods_list[j].goodsid) {
                            shopCartList[i].num += goods_list[j].num;
                            if(userId!=''){
                                cartGoods = {
                                    cartid:shopCartList[i].cartid,
                                    num:shopCartList[i].num,
                                    skuid:shopCartList[i].skuid
                                }
                                MallAuthService.updateCartByCartId(cartGoods,function(){
                                },function(){
                                    alert("同步账号失败")
                                }) ;
                            }
                            goods_list.splice(j, 1);
                        }
                    }
                }
                for (var k = 0; k < goods_list.length; k++) {
                    var good = goods_list[k];
                    var goods = {
                        cartid: "",
                        cid: good.cid,
                        description: good.description,
                        goodsid: good.goodsid,
                        images: good.images,
                        logo: good.logo,
                        num: good.num,
                        price: good.price,
                        shopid: good.shopid,
                        state: good.state,
                        stock: good.stock,
                        title: good.title,
                        unit: good.unit
                    }
                    if(userId!=''){
                        var cartGoods = {
                            buyerid: userId,
                            shopid: good.shopid,
                            goodsid: good.goodsid,
                            skuid: good.skuid,
                            num: good.num,
                        }
                        MallAuthService.addToCartByUserId(cartGoods,function(){
                        },function(){
                            alert("同步账号失败")
                        }) ;
                    }
                    shopCartList.push(goods);
                }
                $storage.shopcart[shopid] = shopCartList;
            } else {
                $storage.shopcart[shopid] = [];
                for (var i = 0; i < goods_list.length; i++) {
                    var good = goods_list[i];
                    var goods = {
                        cartid: "",
                        cid: good.cid,
                        description: good.description,
                        goodsid: good.goodsid,
                        images: good.images,
                        logo: good.logo,
                        num: good.num,
                        price: good.price,
                        shopid: good.shopid,
                        state: good.state,
                        stock: good.stock,
                        title: good.title,
                        unit: good.unit
                    }
                    if(userId!=''){
                        var cartGoods = {
                            buyerid: userId,
                            shopid: good.shopid,
                            goodsid: good.goodsid,
                            skuid: good.skuid,
                            num: good.num,
                        }
                        MallAuthService.addToCartByUserId(cartGoods,function(){
                        },function(){
                            alert("同步账号失败")
                        }) ;
                    }
                    $storage.shopcart[shopid].push(goods);
                }
            }
        }
        //获取商品详情
        this.getGoodsDetail = function (shopid, cid, goodsid, num) {
            var goodsList = menuList[shopid];
            if (goodsList == undefined) {
                goodsList = shop_goods_list[shopid];
            }
            var goodsitem = null;
            for (var i = 0; i < goodsList.length; i++) {
                goodsitem = goodsList[i];
                if (goodsitem.goods == undefined && goodsitem.goodsid == goodsid) {
                    good = goodsitem;
                    break;
                }
                if (goodsitem.goods != undefined && goodsitem.cid == cid) {
                    break;
                }
            }
            if (goodsitem.goods != undefined) {
                var good = null;
                for (var j = 0; j < goodsitem.goods.length; j++) {
                    good = goodsitem.goods[j];
                    if (good.goodsid == goodsid) {
                        if (num != undefined) {
                            goodsitem.goods[j].num = num;
                        }
                        good.cid = cid;
                        good.shopid = shopid;
                        good.images = "";
                        good.state = 0;
                        good.cartid = "";
                        break;
                    }
                }
            }
            return {goods: good};
        }
        //获取商品购买数量
        this.getBuycountByGoodsid = function (shopid, cid, goodsid) {
            var goods_list = [];
            if (shop_goods_list[shopid]) {
                goods_list = shop_goods_list[shopid];
            }
            for (var i = 0; i < goods_list.length; i++) {
                var goodsitem = goods_list[i];
                if (goodsitem.cid == cid && goodsitem.goodsid == goodsid) {
                    return goodsitem.num;
                }
            }
            return 0;
        }
    }])
